import { BaseController } from '#server/common/index.js';

class Controller extends BaseController {
  // 首页
  main() {
    this.$ctx.response.set({
      'Content-Type': 'text/html; charset=utf-8',
      'Cache-Control': 'no-cache',
    });
    this.$render('index');
  }

  // 把相关 token 种到 AtomGit 域下
  transformAtomgitToken() {
    const params = this.$ctx.request.body || {};
    const cookieOptions = {
      path: '/',
      domain: '.atomgit.com',
      expires: new Date(Date.now() + 3600000 * 24 * 180),
      httpOnly: true,
      secure: true,
    };

    params.accessToken && this.$ctx.response.cookie('ATOMGIT_ACCESS_TOKEN', params.accessToken, cookieOptions);
    params.expiresAt && this.$ctx.response.cookie('ATOMGIT_EXPIRES_AT', params.expiresAt, cookieOptions);
    params.expiresIn && this.$ctx.response.cookie('ATOMGIT_EXPIRES_IN', params.expiresIn, cookieOptions);
    params.idToken && this.$ctx.response.cookie('ATOMGIT_ID_TOKEN', params.idToken, cookieOptions);
    params.refreshToken && this.$ctx.response.cookie('ATOMGIT_REFRESH_TOKEN', params.refreshToken, cookieOptions);

    this.$ctx.response.set({
      'Content-Type': 'application/json; charset=utf-8',
      'Cache-Control': 'no-cache',
    });
    this.$ctx.response.status(200).send({ code: 200 });
  }

  // 清理 AtomGit Cookie
  clearAtomgitToken() {
    const cookieOptions = {
      path: '/',
      domain: '.atomgit.com',
      httpOnly: true,
      secure: true,
    };

    this.$ctx.response.clearCookie('ATOMGIT_ACCESS_TOKEN', cookieOptions);
    this.$ctx.response.clearCookie('ATOMGIT_EXPIRES_AT', cookieOptions);
    this.$ctx.response.clearCookie('ATOMGIT_EXPIRES_IN', cookieOptions);
    this.$ctx.response.clearCookie('ATOMGIT_ID_TOKEN', cookieOptions);
    this.$ctx.response.clearCookie('ATOMGIT_REFRESH_TOKEN', cookieOptions);

    this.$ctx.response.set({
      'Content-Type': 'application/json; charset=utf-8',
      'Cache-Control': 'no-cache',
    });
    this.$ctx.response.status(200).send({ code: 200 });
  }
}

export default Controller;
